package com.itheima.test03_1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/*
## 题目3（加强训练）
需求：编写程序，使用UDP协议的发送端给接收端发送一句话：“服务端你好啊！”；然后接收端给发送端返回一句话，内容随意。
 */
public class TestUdpClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(22222);//我要从哪里接收
        InetAddress address = InetAddress.getByName("127.0.0.1");
        int port=11111;
        byte[] bytes = "服务端你好啊！".getBytes();
        DatagramPacket packet = new DatagramPacket(bytes,bytes.length,address,port);//port:我要发送到哪里
        socket.send(packet);

        //还不能下班,它还要收服务器的东西!
        byte[] bytes1 = new byte[1024];
        DatagramPacket packet2 = new DatagramPacket(bytes1,bytes.length);

        socket.receive(packet2);
        System.out.println(new String(bytes1,0,packet2.getLength()));
        socket.close();

    }
}
